//
//  GYSettingViewController.m
//  research
//
//  Created by 章鱼 on 17/3/30.
//  Copyright © 2017年 simon. All rights reserved.
//

#import "GYSettingViewController.h"
#import "GYSettingView.h"
#import "GYRootViewController.h"
#import "GYBaseNavViewController.h"
#import "AppDelegate.h"

@interface GYSettingViewController ()

@property (nonatomic, strong)GYSettingView *notifyView;
@property (nonatomic, strong)GYSettingView *updateView;
@property (nonatomic, strong)GYSettingView *cleanView;
@property (nonatomic, strong)GYSettingView *quitView;

@property (nonatomic, strong)UIView *becomeQuitView;
@property (nonatomic, strong) UIView *backView;
@property (nonatomic, strong) UILabel *topLable;
@property (nonatomic, strong) UIView *firstLineView;
@property (nonatomic, strong) UILabel *updateLable;
@property (nonatomic, strong) UIView *secondLineView;
@property (nonatomic, strong) UIButton *leftButton;
@property (nonatomic, strong) UIButton *rightButton;
@property (nonatomic, strong) UIView *thirdLineView;

@property (nonatomic, strong) GYRootViewController *rootCtrl;
@property (nonatomic, strong) GYBaseNavViewController *rootNavCtrl;

@end

@implementation GYSettingViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self basicSetup];
    [self initSettingView];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self addBackButtonItemWithTitle:@"返回" backImg:@"navigation-bar_back_normal@2x"];

}


- (void)basicSetup {
    [self initNavBarWithTitle:@"设置" tintColor:[UIColor orz_mainColor]];
    [self.navBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
                                         [UIColor whiteColor],NSForegroundColorAttributeName,
                                         [UIFont boldSystemFontOfSize:20],
                                         NSFontAttributeName,
                                         nil]];
    [self.view setBackgroundColor:[UIColor orz_globalBackground]];
}

-(void)initSettingView {
    _notifyView = [[GYSettingView alloc] init];
    [self.view addSubview:_notifyView];
    _notifyView.icon.image = [UIImage imageNamed:@"sttings_icon_notice"];
    _notifyView.wordLable.text = @"接收消息通知";

    //_updateView = [[GYSettingView alloc] init];
    //[self.view addSubview:_updateView];
    //_updateView.icon.image = [UIImage imageNamed:@"sttings_icon_to-update"];
    //_updateView.wordLable.text = @"检查更新";
    //_updateView.nextIconButton.hidden = YES;
    
    _cleanView = [[GYSettingView alloc] init];
    [self.view addSubview:_cleanView];
    _cleanView.icon.image = [UIImage imageNamed:@"sttings_icon_eliminate"];
    _cleanView.wordLable.text = @"清除缓存";
    _cleanView.nextIconButton.hidden = YES;
    _cleanView.lineView.hidden = YES;
    
    _quitView = [[GYSettingView alloc] init];
    [self.view addSubview:_quitView];
    _quitView.icon.image = [UIImage imageNamed:@"sttings_icon_close"];
    _quitView.wordLable.text = @"退出登录";
    _quitView.nextIconButton.hidden = YES;
    _quitView.lineView.hidden = YES;
    _quitView.backgroundColor = [UIColor orz_mainColor];
    _quitView.wordLable.textColor = [UIColor whiteColor];
    UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(quitTap:)];
    [_quitView addGestureRecognizer:tap];
    
    [self initSettingViewUI];
}

- (void)initSettingViewUI {

    [_notifyView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(84.f);
        make.left.mas_equalTo(0.f);
        make.right.mas_equalTo(0.f);
        make.height.mas_equalTo(44.f);
        make.width.mas_equalTo(SCREEN_WIDTH);
        
    }];
    
    //[_updateView mas_makeConstraints:^(MASConstraintMaker *make) {
        //make.top.mas_equalTo(_notifyView.mas_bottom);
       // make.left.mas_equalTo(0.f);
       // make.right.mas_equalTo(0.f);
        //make.height.mas_equalTo(44.f);
        
   // }];
    
    [_cleanView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_notifyView.mas_bottom);
        make.left.mas_equalTo(0.f);
        make.right.mas_equalTo(0.f);
        make.height.mas_equalTo(44.f);
    }];
    
    [_quitView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_cleanView.mas_bottom).offset(30.f);
        make.left.mas_equalTo(0.f);
        make.right.mas_equalTo(0.f);
        make.height.mas_equalTo(44.f);
    }];
}

- (void)quitTap:(UITapGestureRecognizer *)tap {

    [self quitView];
}

- (UIView *)quitView {
    _becomeQuitView = [[UIView alloc] init];
    [self.view addSubview:_becomeQuitView];
    _becomeQuitView.backgroundColor = [UIColor colorWithHex:@"000000" alpha:0.13];
    
    _backView = [[UIView alloc] init];
    [_becomeQuitView addSubview:_backView];
    _backView.backgroundColor = [UIColor whiteColor];
    _backView.layer.cornerRadius = 4.f;
    _backView.layer.masksToBounds = YES;
    
    _topLable = [[UILabel alloc] init];
    [_backView addSubview:_topLable];
    _topLable.textColor = [UIColor colorWithHex:@"323232" alpha:1];
    _topLable.text = @"温馨提示";
    _topLable.font = [UIFont systemFontOfSize:15];
    
    _firstLineView = [[UIView alloc] init];
    [_backView addSubview:_firstLineView];
    _firstLineView.backgroundColor = [UIColor orz_line];
    
    _updateLable = [[UILabel alloc] init];
    [_backView addSubview:_updateLable];
    _updateLable.textColor = [UIColor colorWithHex:@"323232" alpha:1];
    _updateLable.text = @"是否退出登录";
    _updateLable.font = [UIFont systemFontOfSize:13];
    
    _secondLineView = [[UIView alloc] init];
    [_backView addSubview:_secondLineView];
    _secondLineView.backgroundColor = [UIColor orz_line];
    
    _leftButton = [[UIButton alloc] init];
    [_backView addSubview:_leftButton];
    [_leftButton setTitle:@"是" forState:UIControlStateNormal];
    _leftButton.titleLabel.font = [UIFont systemFontOfSize:13];
    [_leftButton setTitleColor:[UIColor colorWithHex:@"323232" alpha:1] forState:UIControlStateNormal];
    [_leftButton addTarget:self action:@selector(leftCLick:) forControlEvents:UIControlEventTouchUpInside];
    
    _rightButton = [[UIButton alloc] init];
    [_backView addSubview:_rightButton];
    [_rightButton setTitle:@"否" forState:UIControlStateNormal];
    _rightButton.titleLabel.font = [UIFont systemFontOfSize:13];
    [_rightButton setTitleColor:[UIColor colorWithHex:@"323232" alpha:1] forState:UIControlStateNormal];
    [_rightButton addTarget:self action:@selector(rightCLick:) forControlEvents:UIControlEventTouchUpInside];
    
    _thirdLineView = [[UIView alloc] init];
    [_backView addSubview:_thirdLineView];
    _thirdLineView.backgroundColor = [UIColor orz_line];
    
    [self setQuitViewUI];
    return _becomeQuitView;
}
- (void)setQuitViewUI {
    
    [_becomeQuitView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view);
    }];
    
    [_backView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(164.f);
        make.left.mas_equalTo(50.f);
        make.right.mas_equalTo(-50.f);
    }];
    
    [_topLable mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(_backView);
        make.top.mas_equalTo(20.f);
        
    }];
    
    [_firstLineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(44.f);
        make.left.mas_equalTo(20.f);
        make.right.mas_equalTo(-20.f);
        make.height.mas_equalTo(0.5f);
    }];
    
    [_updateLable mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_firstLineView.mas_bottom).offset(30.f);
        make.centerX.mas_equalTo(_backView);
    
    }];
    
    [_secondLineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_firstLineView.mas_bottom).offset(72.f);
        make.left.mas_equalTo(20.f);
        make.right.mas_equalTo(-20.f);
        make.height.mas_equalTo(0.5f);
        
    }];
    
    [_thirdLineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(_backView);
        make.height.mas_equalTo(33.f);
        make.width.mas_equalTo(1.f);
        make.top.mas_equalTo(_secondLineView.mas_bottom);
        
    }];
    
    [_leftButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_secondLineView.mas_bottom);
        make.left.mas_equalTo(0.f);
        make.bottom.mas_equalTo(0.f);
        make.height.mas_equalTo(44.f);
        make.right.mas_equalTo(_thirdLineView.mas_left);
    }];
    
    [_rightButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_secondLineView.mas_bottom);
        make.right.mas_equalTo(0.f);
        make.bottom.mas_equalTo(0.f);
        make.height.mas_equalTo(44.f);
        make.left.mas_equalTo(_thirdLineView.mas_right);
    }];
}

-(void)leftCLick:(UIButton *)btn {
    
    [self dismissSlectedView];
    [[GYConfigDBManager defaultManager] cleanUserData];
    _rootCtrl = [[GYRootViewController alloc] init];
    _rootNavCtrl = [[GYBaseNavViewController alloc] initWithRootViewController:_rootCtrl];
    [_rootNavCtrl.navigationBar setHidden:YES];
    [AppDelegate shareDelegate].window.rootViewController = _rootNavCtrl;
    [[AppDelegate shareDelegate].window makeKeyAndVisible];
    
}

-(void)rightCLick:(UIButton *)btn {
    [self dismissSlectedView];

}

/**
 *  重写touchesEnded方法
 */
- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    if ([touches anyObject].view != _backView) {
        // 判断点击的区域如果不是菜单按钮_btnMenu, 则关闭菜单
        [self dismissSlectedView];
    }
}
- (void)dismissSlectedView {
    if (_becomeQuitView) {
        [_becomeQuitView removeFromSuperview];
        
    }
}



@end
